<?php

use Biz\model\ShopCategoryClassify;
use Biz\service\BrandPreFecture;

/**
 * 品牌调查局
 */
class InvestigationAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;

    const ALL_DATA = [
        1 => [
            'tab_id' => 1,
            'tab_name' => '火星人集成灶',
            'cover_img' => 'https://img.haohaozhu.cn/Op-imageShow/bbfd730i80ie0Pk00ry4rqg?iv=1',
            'url' => 'https://lego.haohaozhu.cn/lego/514.html',
            'brand_id' => 827,
        ],
    ];

    public function main()
    {
        $tabId = $this->getRequest()->getQuery('tab_id', 0);
        if (0 == $tabId) {
            $tabId = rand(1, count(self::ALL_DATA));
        }
        $data = [
            'tabs' => [],
            'tab_info' => null,
            'brand_info' => null,
            'wiki_list' => [],
        ];
        foreach (self::ALL_DATA as $row) {
            $tabInfo = [
                'tab_id' => $row['tab_id'],
                'tab_name' => $row['tab_name'],
                'cover_img' => $row['cover_img'],
                'link' => $this->getLink($row['url'])
            ];
            $data['tabs'][] = $tabInfo;
            if ($tabId == $row['tab_id']) {
                $data['tab_info'] = $tabInfo;
            }
        }
        $tabData = self::ALL_DATA[$tabId] ?? [];
        if ($tabData) {
            $brandId = $tabData['brand_id'];
            $brandInfos = \Biz\service\brand\Brand::getBrandUserInfos([$brandId], true, false, true);
            $brandInfo = $brandInfos[$brandId] ?? [];
            $data['brand_info'] = $brandInfo ?: null;
            $res = \Biz\service\brand\BrandCustomCategory::getWikiByCategoryId($brandId, $this->uid, 1, 0);
            $i = 0;
            foreach ($res['list'] as $row) {
                if ($i < 6) {
                    $row['statSign'] = '';
                    $data['wiki_list'][] = $row;
                }
                $i++;
            }
        }
        $this->setSuccess($data);
    }

    private function getLink($url, $isFullScreen = false)
    {
        if ($isFullScreen) {
            $link = 'niceliving://webview/fullscreen?protego=' . urlencode(base64_encode(json_encode(['url' => $url])));
        } else {
            $link = 'niceliving://webview/normalscreen?protego=' . urlencode(base64_encode(json_encode(['url' => $url])));
        }
        return $link;
    }


}